package com.one.consumer.user.ui;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.utils.ClickUtils;
import com.one.common.view.widget.InputLayout;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.one.consumer.user.model.extra.InvoiceExtra;
import com.one.consumer.user.model.item.LookUpItem;
import com.one.consumer.user.presenter.InvoicePresenter;
import com.one.consumer.user.ui.view.LookUpView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/11/3 10:15 PM
 * description ：添加发票抬头
 **/
@Route(path = RouterPath.ADD_LOOK_UP)
public class AddLookUpActivity extends BaseActivityC<InvoicePresenter> implements LookUpView {
    @BindView(R.id.rb_1)
    RadioButton rb1;
    @BindView(R.id.rb_2)
    RadioButton rb2;
    @BindView(R.id.rg_order_invoice)
    RadioGroup rgOrderInvoice;
    @BindView(R.id.il_company_name)
    InputLayout ilCompanyName;
    @BindView(R.id.il_company_no)
    InputLayout ilCompanyNo;
    @BindView(R.id.il_company_address)
    InputLayout ilCompanyAddress;
    @BindView(R.id.il_company_phone)
    InputLayout ilCompanyPhone;
    @BindView(R.id.il_company_bank)
    InputLayout ilCompanyBank;
    @BindView(R.id.il_company_bank_account)
    InputLayout ilCompanyBankAccount;
    @BindView(R.id.ll_company)
    LinearLayout llCompany;
    @BindView(R.id.il_name)
    InputLayout ilName;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    /**
     *  1:个人 2:公司
     */
    private String invoiceType ="1";
    @Override
    protected void initPresenter() {
        mPresenter = new InvoicePresenter(this,this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_add_look_up;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        InvoiceExtra extra = (InvoiceExtra) getIntent().getSerializableExtra(InvoiceExtra.getExtraName());
        String title = "添加抬头";
        if(extra.getType() == InvoiceExtra.UPDATE){
            title = "编辑抬头";
        }
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText(title);
    }

    @Override
    public void initView() {
        super.initView();
        rgOrderInvoice.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if(i == rb1.getId()){
                    llCompany.setVisibility(View.VISIBLE);
                    ilName.setVisibility(View.GONE);
                    invoiceType = "2";
                } else {
                    llCompany.setVisibility(View.GONE);
                    ilName.setVisibility(View.VISIBLE);
                    invoiceType = "1";
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        InvoiceExtra extra = (InvoiceExtra) getIntent().getSerializableExtra(InvoiceExtra.getExtraName());
        if (extra != null && extra.getLookUpItem() != null) {
            setDefaultLookUp(extra.getLookUpItem());
        }
    }

    @OnClick(R.id.tv_submit)
    public void submit(){
        if(ClickUtils.isFastClick())return;
        mPresenter.submit(invoiceType, ilName.getText(),ilCompanyNo.getText(),
                ilCompanyName.getText(),ilCompanyAddress.getText(),ilCompanyPhone.getText(),
                ilCompanyBank.getText(), ilCompanyBankAccount.getText());
    }

    @Override
    public void setDefaultLookUp(LookUpItem item) {
        if (item.getInvoice_type().equals("1")) { // 个人
            ilName.setText(item.getInvoice_name());
        } else { // 企业
            ilCompanyName.setText(item.getInvoice_company_name());
            ilCompanyAddress.setText(item.getInvoice_company_address());
            ilCompanyBank.setText(item.getInvoice_bank_name());
            ilCompanyBankAccount.setText(item.getInvoice_bank_no());
            ilCompanyPhone.setText(item.getInvoice_company_phone());
            ilCompanyNo.setText(item.getInvoice_no());
        }
    }
}
